﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hi_Baidu.User;
using System.Windows.Controls;

namespace Hi_Baidu.Entry.CollectionObject
{
    public class Friend : MarshalByRefObject, IFriendProcess, IMetaData
    {
        public Friend(String Key)
        {
            this.key = Key;
        }

        public Friend()
        {
        }

        #region 好友属性

        private String infomation;
        private int age;
        private Image headImage;
        private bool onlineState;
        private String name;
        private String ipAddress;
        private int talkPort;
        private int videoPort;
        private int voicePort;
        private int filePort;
        private Item forItem;
        private Level.MessionLevel mession;
        private String username;
        private String prod;
        private String headImageName;
        private TreeViewItem visualTreeItem;
        private TreeViewItem friendNode;
        private bool hasTalkPortHole = false;
        private bool hasFilePortHole = false;
        private bool hasVideoPortHole = false;
        private bool hasVoicePortHole = false;

        /// <summary>
        ///     视频端口
        /// </summary>
        public int VideoPort
        {
            get { return videoPort; }
            set { videoPort = value; }
        }

        /// <summary>
        ///     语音端口
        /// </summary>
        public int VoicePort
        {
            get { return voicePort; }
            set { voicePort = value; }
        }

        /// <summary>
        ///     文件传送端口
        /// </summary>
        public int FilePort
        {
            get { return filePort; }
            set { filePort = value; }
        }

        /// <summary>
        ///     是否聊天端口打洞成功
        /// </summary>
        public bool HasTalkPortHole
        {
            get { return hasTalkPortHole; }
            set { hasTalkPortHole = value; }
        }

        /// <summary>
        ///     是否文件端口打洞成功
        /// </summary>
        public bool HasFilePortHole
        {
            get { return hasFilePortHole; }
            set { hasFilePortHole = value; }
        }

        /// <summary>
        ///     是否视频端口打洞成功
        /// </summary>
        public bool HasVideoPortHole
        {
            get { return hasVideoPortHole; }
            set { hasVideoPortHole = value; }
        }

        /// <summary>
        ///     是否音频端口打洞成功
        /// </summary>
        public bool HasVoicePortHole
        {
            get { return hasVoicePortHole; }
            set { hasVoicePortHole = value; }
        }

        /// <summary>
        ///     当前好友所在好友树节点
        /// </summary>
        public TreeViewItem FriendNode
        {
            get { return friendNode; }
            set { friendNode = value; }
        }

        /// <summary>
        ///     好友图像文件名
        /// </summary>
        public String HeadImageName
        {
            get { return headImageName; }
            set { headImageName = value; }
        }

        /// <summary>
        ///     个人签名
        /// </summary>
        public String Prod
        {
            get { return prod; }
            set { prod = value; }
        }

        /// <summary>
        ///     用户名
        /// </summary>
        public String Username
        {
            get { return username; }
            set { username = value; }
        }

        /// <summary>
        ///     详细备注
        /// </summary>
        public string Infomation
        {
            get { return infomation; }
            set { infomation = value; }
        }

        /// <summary>
        ///     年龄
        /// </summary>
        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        /// <summary>
        ///     头像
        /// </summary>
        public Image HeadImage
        {
            get { return headImage; }
            set { headImage = value; }
        }

        /// <summary>
        ///     在线状态
        /// </summary>
        public bool OnlineState
        {
            get { return onlineState; }
            set { onlineState = value; }
        }

        /// <summary>
        ///     名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        ///     IP地址
        /// </summary>
        public string IpAddress
        {
            get { return ipAddress; }
            set { ipAddress = value; }
        }

        /// <summary>
        ///     聊天端口 [UDP]
        /// </summary>
        public int TalkPort
        {
            get { return talkPort; }
            set { talkPort = value; }
        }

        /// <summary>
        ///     所在分组
        /// </summary>
        public Item ForItem
        {
            get { return forItem; }
            set { forItem = value; }
        }

        /// <summary>
        ///     用户权限
        /// </summary>
        public Level.MessionLevel Mession
        {
            get { return mession; }
            set { mession = value; }
        }

        /// <summary>
        ///      用户在好友树中真实的分组TreeViewItem
        /// </summary>
        public TreeViewItem VisualTreeItem
        {
            get { return visualTreeItem; }
            set { visualTreeItem = value; }
        }

        #endregion

        #region IFriendProcess 成员

        public bool SendMessage(String TalkContent)
        {
            throw new NotImplementedException();
        }

        public void UpdateName()
        {
            throw new NotImplementedException();
        }

        public void UpdateAge()
        {
            throw new NotImplementedException();
        }

        public void UpdateInfomation()
        {
            throw new NotImplementedException();
        }

        public void ChangeOnlineState()
        {
            throw new NotImplementedException();
        }

        public void UpdateHeadImage()
        {
            throw new NotImplementedException();
        }

        public void DigHole(byte[] MySelfUsername, byte[] HolePort, int DigType, String SessionCode, String ServerIpAddress, int ServerPort)
        {
            byte[] UsernameLength = BitConverter.GetBytes(MySelfUsername.Length);
            byte[] username = Encoding.Default.GetBytes(this.username);
            byte[] FriendnameLength = BitConverter.GetBytes(username.Length);
            byte[] packagebody = new byte[MySelfUsername.Length + username.Length + 16];
            int Offset = 0;
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(MySelfUsername, 0, packagebody, Offset, MySelfUsername.Length);
            Offset += MySelfUsername.Length;
            Array.ConstrainedCopy(FriendnameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(username, 0, packagebody, Offset, username.Length);
            Offset += username.Length;
            Array.ConstrainedCopy(HolePort, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(BitConverter.GetBytes(DigType), 0, packagebody, Offset, 4);
            Offset += 4;
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.ReGetProtocolToTalkMessage("DigHole",
                                                                       (int)Packet.Protocol.TALK_OUT_NetworkCommand.DIG_A_HOLE,
                                                                       (int)Packet.Protocol.NetworkProtocol.TALK_MESSAGE,
                                                                       packagebody,
                                                                       ProtocolFactory.ProtocolFactory.ACK.Yes,
                                                                       ProtocolFactory.ProtocolFactory.ENCRYPT.No,
                                                                       SessionCode);
            NetControl.Send(protocol1, ServerPort, ServerIpAddress);
        }

        #endregion

        #region IMetaData 成员

        private String key;
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }

        #endregion
    }
}
